package org.odk.clinic.android.adapters;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;


import org.odk.clinic.android.R;

import custom_View.aperson;


import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class PatientAdapter1  extends ArrayAdapter<aperson>{
	private TextView tv1,tv2; 
	
	private SimpleDateFormat mDateFormat = new SimpleDateFormat("dd-mm-yyyy");
	
	//private static ArrayList<aperson> result;
	
	public PatientAdapter1(Context context, int textViewResourceId,
			ArrayList<aperson> items) {
		super(context, textViewResourceId, items);
		//result = items;
	}
	
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) getContext()
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.patient_list_item1, null);
		}
		
		aperson ap = getItem(position);
		if (ap != null) {

			TextView tv1 = (TextView)v.findViewById(R.id.identifier_text2);
			if (tv1 != null) {
				tv1.setText(ap.getPatientId().toString());
			}
			tv1 = (TextView)v.findViewById(R.id.name_text2);
			if(tv1 != null){
				StringBuilder nameBuilder = new StringBuilder();
				nameBuilder.append(ap.getGivenName());
				nameBuilder.append(" ");
				nameBuilder.append(ap.getMiddleName());
				nameBuilder.append(" ");
				nameBuilder.append(ap.getFamilyName());
				tv1.setText(nameBuilder.toString());
			}
			tv1 = (TextView)v.findViewById(R.id.birthdate_text2);
			if(tv1 != null){
				tv1.setText(mDateFormat.format(ap.getBirth()));
			}
			ImageView imageView = (ImageView) v.findViewById(R.id.gender_image2);
			if(imageView != null){
				if(ap.getGender().equals("M")){
					imageView.setImageResource(R.drawable.male);
				}
				else if (ap.getGender().equals("F")){
					imageView.setImageResource(R.drawable.female);
				}
			}
				
			
		}
		return v;
	}
}
